// 导入
require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.less')
require('../font/iconfont.css')
require('../css/register.less')

// 引入weui
require('weui')
const weui = require('weui.js')
// 引入axios
const axios = require('axios')


document.addEventListener('DOMContentLoaded', function () {
    // 引入验证码第三方库
    var CaptchaMini = require("captcha-mini")
    // 
    let code = ''

    // 验证码
    let captcha = new CaptchaMini({
        lineWidth: 1, //线条宽度
        lineNum: 0, //线条数量
        dotR: 1, //点的半径
        dotNum: 0, //点的数量
        preGroundColor: [0, 50], //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 50, //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'], //字体类型
        fontStyle: 'fill', //字体绘制方法，有fill和stroke
        content: 'abcdefghijklmnopqrstuvwxyz1234567890', //验证码内容
        length: 6 //验证码长度
    });
    captcha.draw(document.querySelector('#captcha'), function (r) {
        code = r
    });

    // 获取元素
    const tel = document.querySelector('#tel') //手机号
    const telCode = document.querySelector('#telCode') //验证码
    const password = document.querySelector('#password') //密码
    const passwordAgain = document.querySelector('#passwordAgain') // 确认密码
    const submitBtn = document.querySelector('#submitBtn') //注册按钮
    const msg = document.querySelector('#msg') //错误提示
    submitBtn.addEventListener('click', function () {
        // console.log(code);
        // console.log(1);
        // console.log(axios);
        const telValue = tel.value
        const telCodeValue = telCode.value
        const passwordValue = password.value
        const passwordAgainValue = passwordAgain.value
        // console.log(telValue, telCodeValue, passwordValue, passwordAgainValue);
        if (!/^1[3-9][0-9]{9}$/.test(telValue)) {
            msg.textContent = '手机号不正确'
            return;
        } else if (telCode.value != code) {
            msg.textContent = '验证码不正确'
            return;
        } else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordValue)) {
            msg.textContent = '密码不正确'
            return;
        } else if (passwordAgainValue != passwordValue) {
            msg.textContent = '两次密码不一致'
            // console.log(2);
            return;
        } else {
            // console.log(3);
            msg.textContent = ''
            // 给服务器发送请求
            const url = 'http://139.9.177.51:8099/users/add'
            axios.post(url, {
                "account": telValue,
                "password": passwordValue
            }).then(function (res) {
                if (res.data.status === 0) {
                    weui.toast('注册成功', {
                        duration: 2000,
                        className: 'custom-classname',
                        callback: function () {
                            location.href = './login.html'
                        }
                    });

                } else {
                    weui.alert('账号以注册');
                }
            })
        }
    })
})